	<template>
  <view class="bd">
    <p class="banner" v-show="isPtUser">
      <span
        >个人隐私<sup style="font-size: 10px"
          >*开启后切换用户你的信息将在家庭组中被隐藏</sup
        ></span
      >
      <span style="position: relative">
        <span class="tips">{{ isPtUserCode ? "开启" : "关" }}</span>
        <span class="tips t1" v-show="!isPtUserCode">关闭</span>
        <u-switch
          v-model="isPtUserCode"
          active-color="#18c7e1"
          @change="change"
        >
        </u-switch>
      </span>
    </p>
    <u-table>
      <u-tr style="height: 120rpx">
        <u-th class="b1">姓名</u-th>
        <u-th class="b1">电话</u-th>
        <u-th class="b1">关系</u-th>
        <u-th class="b1">操作</u-th>
      </u-tr>
      <u-tr v-for="(item, index) in list" style="height: 90rpx" class="b1">
        <u-td class="b2">{{ item.appUserInfo.userName }}</u-td>
        <u-td class="b2">{{ item.appUserInfo.phone }}</u-td>
        <u-td class="b2">
          <u-tag
            :text="item.realtions || '请选择'"
            mode="plain"
            type="info"
            @click="edits(index)"
          />
        </u-td>
        <u-td class="b2">
          <u-icon
            name="close-circle"
            style="font-size: 48rpx; margin-left: 35%"
            @click="modelbox(index)"
          >
          </u-icon>
        </u-td>
      </u-tr>
    </u-table>
    <u-modal
      v-model="show"
      :content="content"
      @confirm="click(savaidx)"
      show-cancel-button="true"
    ></u-modal>
    <view class="select">
      <u-picker
        v-model="show2"
        mode="selector"
        :range="yblist"
        class="selted"
        range-key="name"
        @confirm="getSelectVal2"
      ></u-picker>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      lists: [], //家庭关系
      realtion: "",
      show2: false,
      selectorObjIndex2: 0,
      selectorObj2: [
        {
          name: "爸爸",
          id: "0",
        },
        {
          name: "妈妈",
          id: "1",
        },
      ],
      idx: "",
      savaidx: "",
      show: false, //弹框状态
      content: "是否删除此家庭成员",
      isPtUser: false, //是否注册用户
      isPtUserCode: false, // 状态
    };
  },
  computed: {
    
  },
  onLoad() {
    this.getInsType();
    this.getFamilyList();
    this.isUser(); //判断是否是注册用户
  },
  onReady() {
    // this.getUiStyle();
  },
  methods: {
    getUiStyle() {
      var style = plus.navigator.getUiStyle();
      if ("dark" == style) {
        console.log("当前为暗黑模式");
        uni.setTabBarStyle({
          backgroundColor: "#1B1C1E",
        });
        uni.setNavigationBarColor({
          frontColor: "#ffffff",
          backgroundColor: "#1B1C1E",
          animation: {
            duration: 400,
            timingFunc: "easeIn",
          },
        });
      } else {
        console.log("当前为普通模式");
      }
    },
    //个人隐私开关
    change(e) {
      console.log(e);
      let flag = e ? 1 : 0;
      this.config
        .post("/family/clickSetting", {
          flag: flag,
        })
        .then((res) => {
          console.log(res);
          if (res.data.code === 200) {
          } else {
            if (res.data.code == "-201") {
              self.config.handleError(res.data);
            } else {
              uni.showToast({
                title: self.config.handleError(res.data),
                icon: "none",
              });
            }
          }
        });
    },
    //判断是否是注册用户
    isUser() {
      this.config.get("/family/pdNowUserIsReg", {}).then((res) => {
        //console.log(res)
        if (res.data.code === 200 && res.data.data === 1) {
          this.isPtUser = true;
          this.isTurn();
        } else {
          if (res.data.code == "-201") {
            self.config.handleError(res.data);
          } else {
            uni.showToast({
              title: self.config.handleError(res.data),
              icon: "none",
            });
          }
        }
      });
    },
    //是否开关
    isTurn() {
      this.config.get("/family/pdNowUserIsVisible", {}).then((res) => {
        console.log(res);
        if (res.data.code === 200) {
          if (res.data.data === 1) {
            this.isPtUserCode = true;
          } else {
            this.isPtUserCode = false;
          }
        } else {
          if (res.data.code == "-201") {
            self.config.handleError(res.data);
          } else {
            uni.showToast({
              title: self.config.handleError(res.data),
              icon: "none",
            });
          }
        }
      });
    },
    getSelectVal(v) {
      this.selectorObjIndex = v[0];
    },
    getSelectVal2(v) {
      this.selectorObjIndex2 = v[0];
      this.realtion = this.selectorObj2[v].dictValue;
      let toUserIds = this.list[this.idx].appUserInfo.id;
      let self = this;
      this.config
        .post("/family/editFamilyRelation", {
          toUserId: toUserIds,
          relation: this.realtion,
        })
        .then((res) => {
          uni.hideLoading();
          if (res.data.code === 200) {
            //console.log(res)
            this.getInsType();
            this.getFamilyList();
          } else {
            if (res.data.code == "-201") {
              self.config.handleError(res.data);
            } else {
              uni.showToast({
                title: self.config.handleError(res.data),
                icon: "none",
              });
            }
          }
        });
    },
    getInsType() {
      let self = this;
      this.config
        .get("/util/sysDictData/type/family_relation", {})
        .then((res) => {
          console.log(res)
          if (res.data.code === 200) {
            self.selectorObj2 = res.data.data;
          } else {
            if (res.data.code == "-201") {
              self.config.handleError(res.data);
            } else {
              uni.showToast({
                title: self.config.handleError(res.data),
                icon: "none",
              });
            }
          }
        });
    },
    //修改关系
    edits(v) {
      this.realtion = "";
      this.show2 = true;
      this.idx = v;
      // if(this.realtion !=''){
      // 	this.list[v].realtions = this.realtion
      // }
    },
    // 获取家庭组列表
    getFamilyList() {
      let self = this;
      uni.showLoading({
        title: "加载中",
        mask: true,
      });
      setTimeout((s) => {
        uni.hideLoading();
      }, 5000);
      this.config.post("/family/getFamilyList", {}).then((res) => {
        uni.hideLoading();
        console.log(res)
		
        if (res.data.code === 200) {
          let arrs = res.data.data;
		  self.list = [];
		  let results = res.data.data.filter((v) => {
		  	return v.appUserInfo
		  })
		  self.list = results;
          if (typeof res.data.data === undefined) {
            uni.showToast({
              title: "暂无关联的家庭关系",
              icon: none,
            });
          }
        } else {
          if (res.data.code == "-201") {
            self.config.handleError(res.data);
          } else {
            uni.showToast({
              title: self.config.handleError(res.data),
              icon: "none",
            });
          }
        }
      });
    },
    modelbox(index) {
      this.show = true;
      this.savaidx = index;
    },
    click(savaidx) {
      let index = this.savaidx;
      let userid = this.list[index].appUserInfo.id;
      //console.log(userid)
      let self = this;
      uni.showLoading({
        title: "加载中",
        mask: true,
      });
      setTimeout((s) => {
        uni.hideLoading();
      }, 5000);
      this.config
        .post("/family/removeFamilyRelation", {
          toUserId: userid,
        })
        .then((res) => {
          //console.log(res)
          uni.hideLoading();
          if (res.data.code === 200) {
            this.$u.toast(`删除成功`);
            this.getFamilyList();
          } else {
            if (res.data.code == "-201") {
              self.config.handleError(res.data);
            } else {
              uni.showToast({
                title: self.config.handleError(res.data),
                icon: "none",
              });
            }
          }
        });
    },
  },
};
</script>

<style lang="scss" scoped>
span.tips {
  position: absolute;
  left: 6px;
  top: 22%;
  z-index: 1;
  font-size: 10px;
  zoom: 0.9;
  color: #ffffff;
}
.t1 {
  left: 28px !important;
  color: #000000 !important;
}
.banner {
  padding: 20rpx;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
</style>
<style scoped>
@media (prefers-color-scheme: closedark) {
  .bd {
    height: calc(100vh - var(--window-top));
    background-color: #1b1c1e !important;
    color: #f8f8f8 !important;
  }

  .b1 {
    background-color: #000000 !important;
    color: #f8f8f8 !important;
  }

  .b2 {
    color: #f8f8f8 !important;
  }
}
</style>